package com.yimixian.app.address;

import android.graphics.Bitmap;

import java.util.List;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.ta.utdid2.android.utils.StringUtils;

/**
 * @ClassName: BaiduMapUtilByRacer
 * @Description: 百度地圖工具類
 *
 */
public class BaiduMapUtilByRacer {

	/**
	 * @Title: showInfoWindowByBitmap
	 * @Description: 通過bitmap來顯示InfoWindow
	 * @param lat
	 * @param lon
	 * @param mBaiduMap
	 * @param distance
	 * @param isMoveTo
	 * @param listener
	 * @return
	 * @return InfoWindow
	 * @throws
	 */
	public static InfoWindow showInfoWindowByBitmap(double lat, double lon,
													Bitmap mBitmap, BaiduMap mBaiduMap, int distance, boolean isMoveTo,
													InfoWindow.OnInfoWindowClickListener listener) {
		InfoWindow mInfoWindow = new InfoWindow(mBitmap == null ? null
				: BitmapDescriptorFactory.fromBitmap(mBitmap), new LatLng(lat,
				lon), distance, listener);
		mBaiduMap.showInfoWindow(mInfoWindow);
		if (isMoveTo) {
			moveToTarget(lat, lon, mBaiduMap);
		}
		return mInfoWindow;
	}

	public static GeoCoder mGeoCoder = null;
	public static GeoCodeListener mGeoCodeListener = null;

	public interface GeoCodeListener {
		void onGetSucceed(LocationBean locationBean);

		void onGetFailed();
	}


	public static GeoCodePoiListener mGeoCodePoiListener = null;

	public interface GeoCodePoiListener {
		void onGetSucceed(LocationBean locationBean, List<PoiInfo> poiList);

		void onGetFailed();
	}

	/**
	 * @Title: getPoiByGeoCode
	 * @Description: 根据经纬度获取周边热点名
	 * @param lat
	 * @param lon
	 * @param listener
	 * @return void
	 * @throws
	 */
	public static void getPoisByGeoCode(double lat, double lon,
			GeoCodePoiListener listener) {
		mGeoCodePoiListener = listener;
		if (mGeoCoder == null) {
			mGeoCoder = GeoCoder.newInstance();
		}
		mGeoCoder.setOnGetGeoCodeResultListener(new MyGeoCodeListener());
		// 反Geo搜索
		mGeoCoder.reverseGeoCode(new ReverseGeoCodeOption()
				.location(new LatLng(lat, lon)));
	}

	/**
	 * @ClassName: MyGeoCodeListener
	 * @Description: geo搜索的回調
	 *
	 */
	public static class MyGeoCodeListener implements
			OnGetGeoCoderResultListener {

		@Override
		public void onGetGeoCodeResult(GeoCodeResult result) {
			if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
				if (mGeoCodeListener != null) {
					LocationBean bean = new LocationBean();
					bean.latitude = result.getLocation().latitude;
					bean.longitude = result.getLocation().longitude;
					mGeoCodeListener.onGetSucceed(bean);
				}
				if (mGeoCodePoiListener != null) {
					mGeoCodePoiListener.onGetFailed();
				}
				destroyGeoCode();
				return;
			}
			// 反Geo搜索
			mGeoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(result
					.getLocation()));
		}

		@Override
		public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
			if (result==null&&result.error != SearchResult.ERRORNO.NO_ERROR){
				if (mGeoCodePoiListener != null) {
					mGeoCodePoiListener.onGetFailed();
				}
				return;
			}
			LocationBean mLocationBean = new LocationBean();
			if(result.getAddressDetail()!=null){
				mLocationBean.setProvince(result.getAddressDetail().province);
				mLocationBean.setCity(result.getAddressDetail().city);
				mLocationBean.setDistrict(result.getAddressDetail().district);
				mLocationBean.setLocName(result.getAddressDetail().street);
				mLocationBean.setStreet(result.getAddressDetail().street);
				mLocationBean.setStreetNum(result.getAddressDetail().streetNumber);
			}
			mLocationBean.mPoiInfoList = result.getPoiList();
			if(result.getLocation()!=null){
				mLocationBean.setLatitude(result.getLocation().latitude);
				mLocationBean.setLongitude(result.getLocation().longitude);
			}
			if (mGeoCodeListener != null) {
				mGeoCodeListener.onGetSucceed(mLocationBean);
			}
			if (mGeoCodePoiListener != null) {
				mGeoCodePoiListener.onGetSucceed(mLocationBean,
						result.getPoiList());
			}
			destroyGeoCode();
		}
	}
	/**
	 * @Title: destroyGeoCode
	 * @Description: 銷毀及置空geo搜索相關對象
	 * @return void
	 * @throws
	 */
	public static void destroyGeoCode() {
		if (mGeoCoder != null) {
			mGeoCoder.destroy();
			mGeoCoder = null;
		}
		mGeoCodeListener = null;
		mGeoCodePoiListener = null;
	}


	/**
	 * @Title: showMarkerByResource
	 * @Description: 通過resource來顯示Marker
	 * @param lat
	 * @param lon
	 * @param mBaiduMap
	 * @param distance
	 * @param isMoveTo
	 * @return Marker
	 * @throws
	 */
	public static Marker showMarkerByResource(double lat, double lon,
											  int resource, BaiduMap mBaiduMap, int distance, boolean isMoveTo) {
		BitmapDescriptor bdView = BitmapDescriptorFactory
				.fromResource(resource);
		OverlayOptions ooView = new MarkerOptions()
				.position(new LatLng(lat, lon)).icon(bdView).zIndex(distance)
				.draggable(true);
		if (isMoveTo) {
			moveToTarget(lat, lon, mBaiduMap);
		}
		return (Marker)mBaiduMap.addOverlay(ooView);
	}
	/**
	 * @Title: moveToTarget
	 * @Description:移動到該點
	 * @param lat
	 * @param mBaiduMap
	 * @return void
	 * @throws
	 */
	public static void moveToTarget(double lat, double lon, BaiduMap mBaiduMap) {
		mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(new LatLng(
				lat, lon)));
	}

	/**
	 * @Title: getLocationByGeoCode
	 * @Description: 獲取坐標通過geo搜索
	 * @param mLocationBean
	 * @param listener
	 * @return void
	 * @throws
	 */
	public static void getLocationByGeoCode(LocationBean mLocationBean,
											GeoCodeListener listener, GeoCodePoiListener poiListener){
		mGeoCodeListener = listener;
		mGeoCodePoiListener = poiListener;
		if (mLocationBean == null || mLocationBean.getCity() == null
				|| mLocationBean.getLocName() == null) {
			if (mGeoCodeListener != null) {
				mGeoCodeListener.onGetFailed();
			}
			destroyGeoCode();
			return;
		}
		if (mGeoCoder == null) {
			mGeoCoder = GeoCoder.newInstance();
		}
		mGeoCoder.setOnGetGeoCodeResultListener(new MyGeoCodeListener());
		// Geo搜索
		mGeoCoder.geocode(new GeoCodeOption().city(mLocationBean.getCity())
				.address(mLocationBean.getLocName()));
	}

	/**
	 * 有UId的情况下匹配选中的PoiInfo
	 * @param defaultPoiInfos
	 * @param selectedAddress
	 * @return
	 */
	public static List<PoiInfo> getSortPoiInfos(List<PoiInfo> defaultPoiInfos,
												MutableAddress selectedAddress,
												PoiInfo currentSearchPoiInfo){
		if (currentSearchPoiInfo != null){
			PoiInfo selectedPoiInfo = null;
			for (int i = 0; i < defaultPoiInfos.size(); i++){
				PoiInfo poiInfo = defaultPoiInfos.get(i);
				if (currentSearchPoiInfo.uid.equals(poiInfo.uid)){
					selectedPoiInfo = defaultPoiInfos.get(i);
					break;
				}
			}
			if (selectedPoiInfo != null){
				defaultPoiInfos.remove(selectedPoiInfo);
				defaultPoiInfos.add(0, selectedPoiInfo);
			}else {
				defaultPoiInfos.add(0, currentSearchPoiInfo);
			}
			return defaultPoiInfos;
		}else if (defaultPoiInfos != null && defaultPoiInfos.size() > 0
				&& selectedAddress != null
				&& !StringUtils.isEmpty(selectedAddress.poiId)){
			PoiInfo selectedPoiInfo = null;
			for (int i = 0; i < defaultPoiInfos.size(); i++){
				PoiInfo poiInfo = defaultPoiInfos.get(i);
				if (selectedAddress.poiId.equals("bd-" + poiInfo.uid)){
					selectedPoiInfo = defaultPoiInfos.get(i);
					break;
				}
			}
			if (selectedPoiInfo != null){
				defaultPoiInfos.remove(selectedPoiInfo);
				defaultPoiInfos.add(0, selectedPoiInfo);
			}
			return defaultPoiInfos;
		}else{
			return  defaultPoiInfos;
		}
	}

//	/**
//	 * 获取pointCount位小数点(直接截取)
//	 * @param oldL
//	 * @param pointCount
//	 * @return
//	 */
//	public static double getSixPoint(double oldL, int pointCount){
//		BigDecimal bg = new BigDecimal(oldL);
//		return bg.setScale(pointCount, BigDecimal.ROUND_DOWN).doubleValue();
//	}
}
